<?php
	/** 
	 * Copyright 2009 Bruno Leles
	 * 
	 * Licensed under the Apache License, Version 2.0 (the "License");
	 * you may not use this file except in compliance with the License.
	 * You may obtain a copy of the License at
	 * 
	 * 		http://www.apache.org/licenses/LICENSE-2.0
	 * 
	 * Unless required by applicable law or agreed to in writing, software
	 * distributed under the License is distributed on an "AS IS" BASIS,
	 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	 * See the License for the specific language governing permissions and
	 * limitations under the License.
	 */
	 
	/**
	 *  PHP cURL class, this class provides a simple and reusable interface to curl methods avaible in php
	 * ---- BETTER DESCRIPTION NEEDED ----
	 * @version 0.1 preview
	 * @author Bruno Leles
	 * @link http://br.php.net/manual/en/ref.curl.php
	 */
	class cURL
	{
		/**
		 * Firefox 1.0
		 */
		const FF_100				= 'Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0';
		
		/**
		 * Firefox 2.0.9
		 */
		const FF_209				= 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.9.9';
		
		/**
		 * Firefox 2.6.0
		 */
		const FF_360				= 'Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.2) Gecko/20100101 Firefox/3.6';
		
		/**
		 * Internet Explorer 5.0
		 */
		const IE_550				= 'Mozilla/4.0 (compatible; MSIE 5.50; Windows NT; SiteKiosk 4.8)';
		
		/**
		 * Internet Explorer 6.0
		 */
		const IE_600				= 'Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)';
		
		/**
		 * Internet Explorer 7.0
		 */
		const IE_700				= 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)';
		
		/**
		 * Internet Explorer 8.0
		 */
		const IE_800				= 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.2; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0)';
		
		
		/**
		 * 
		 */
		protected $_userAgent			= cURL::FF_360;
		
		/**
		 * 
		 */
		protected $_referer				= 'http://google.com';
		
		/**
		 * 
		 */
		protected $_usePost				= false;
		
		/**
		 * 
		 */
		protected $_postData			= NULL;
		
		/**
		 * 
		 */
		protected $_url					= NULL;
		
		/**
		 * 
		 */
		protected $_useAuth				= false;
    	
		/**
		 * 
		 */
		protected $_authName      		= '';
    	
		/**
		 * 
		 */
		protected $_authPass      		= '';
		
		/**
		 * 
		 */
		protected $_maxRedirects		= 5;
		
		/**
		 * 
		 */
		protected $_followlocation		= true;
		
		/**
		 * 
		 */
		protected $_cookieFileLocation	= NULL;
		
		/**
		 * 
		 */
		protected $_storedOpts			= NULL;
		
		/**
		 * 
		 */
		protected $_timeout				= 30;
		
		/**
		 * 
		 */
		protected $_response			= NULL;
		
		/**
		 * 
		 */
		protected $_status				= NULL;
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @param Boolean $useAuth
		 * @param String $userName
		 * @param String $userPass
		 * @return Boolean 
		 */
		public function useAuth($useAuth, $userName = '', $userPass = '')
		{
			if($useAuth = $this->setUseAuth($useAuth))
			{
				$this->setUserName($userName);
				$this->setUserPass($userPass);
			}
			return $useAuth;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @param Boolean $value
		 * @return Boolean 
		 */
		public function setUseAuth($value)
		{
			return $this->_useAuth = $value;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @param String $value
		 * @return String 
		 */
		public function setUserName($value)
		{
			return $this->_userName = $value;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @param String $value
		 * @return String 
		 */
		public function setUserPass($value)
		{
			return $this->_userPass = $value;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @param String $value
		 * @return String 
		 */
		public function setReferer($value)
		{
			return $this->_referer = $value;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @param String $value
		 * @return String 
		 */ 
		public function setUserAgent($value)
		{
			return $this->_userAgent = $value;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @param Array $value
		 * @return Array 
		 */
		public function setPostData($value)
		{
			$usePost = true;
			if(is_null($value) || $value === false)
			{
				$usePost = false;
				$value = NULL;
			}
			$this->_usePost = $usePost;
			return $this->_postData = $value;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @param String $value
		 * @return String 
		 */
		public function setURL($value)
		{
			return $this->_url = $value;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @param String $fieldName
		 * @param Mixed $fieldValue
		 * @return void 
		 */
		public function addPostField( $fieldName, $fieldValue )
		{
			if(is_null($this->_postData))
			{
				$this->setPostData( array() );
			}
			$this->_postData[$fieldName] = $fieldValue;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @return String 
		 */
		public function getResponse()
		{
			return $this->_response;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @return int 
		 */
		public function getStatus()
		{
			return $this->_status;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @return String 
		 */
		public function getUserName()
		{
			return $this->_userName;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @return String 
		 */
		public function getUserPass()
		{
			return $this->_userPass;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @return Array 
		 */
		public function getPostData()
		{
			return $this->_postData;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @return Mixed 
		 */
		public function getPostField($fieldName)
		{
			return is_null($this->_postData[$fieldName]) ? NULL : $this->_postData[$fieldName];
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @return String 
		 */
		public function getURL()
		{
			return $this->_url;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @return Boolean
		 */
		public function getSuccess()
		{
			return $this->_status == 200;
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @return Mixed
		 */
		public function getInfo($opt)
		{
			return $this->_storedOpts[$opt];
		}
		
		/** 
		 * --- DESCRIPTION NEEDED --
		 * 
		 * @param String $url
		 * @param Array $storeOpts
		 * @return void 
		 */
		public function execute($url = NULL, $storeOpts = NULL)
		{
			if(!is_null($url))
			{
				$this->_url = $url;
			}
			
			$curl = curl_init();
			
			curl_setopt($curl, CURLOPT_URL, $this->_url);
			curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
			curl_setopt($curl, CURLOPT_TIMEOUT, $this->_timeout);
			curl_setopt($curl, CURLOPT_MAXREDIRS, $this->_maxRedirects);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($curl, CURLOPT_FOLLOWLOCATION, $this->_followlocation);
			curl_setopt($curl, CURLOPT_COOKIEJAR, $this->_cookieFileLocation);
			curl_setopt($curl, CURLOPT_COOKIEFILE, $this->_cookieFileLocation);
			curl_setopt($curl, CURLOPT_USERAGENT, $this->_userAgent);
        	curl_setopt($curl, CURLOPT_REFERER, $this->_referer);
		 	
			if($this->_useAuth)
			{
				curl_setopt($curl, CURLOPT_USERPWD, $this->_authName.':'.$this->_authPass);
			}
			
			if($this->_usePost)
			{
				curl_setopt($curl, CURLOPT_POST, true);
             	curl_setopt($curl, CURLOPT_POSTFIELDS, $this->_postData);
			}
			
			$this->_response = curl_exec($curl);
			
			$this->_url		= curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
			$this->_status	= curl_getinfo($curl, CURLINFO_HTTP_CODE);
			
			$this->_storedOpts = array();
			if(!is_null($storeOpts))
			{
				for( $i=0, $n=count($storeOpts); $i < $n; $i++ )
				{
					$opt = $storeOpts[$i];
					$this->_storedOpts[$opt] = curl_getinfo($curl, $opt);
				}
				unset($i, $n, $opt);
			}
			
			curl_close($curl);
		}
	}
	
?>